<?php
class MapManagement extends SBKH{
	/* 
	 * Show all province info
	 * Param: No
	 */
	public function index(){
		$this -> provinces();
	}
	/*
	 * Show all province info
	 * Param: no
	 */
	public function provinces(){
		if(!isset($_SESSION['user'])) $this->redirect('login');
		$this->SetLayout('default');
		$obj = $this -> LoadModel('mapManagement');
		$records = $obj -> getRecords();
		$this -> SetVar('records',$records);
		$this -> SetVar('page_title', "List Provinces");
		$this -> SetVar('menu_focus', 'list-provinces');
		$this -> View('index', 'left-menu');
	}
	/*
	 * Search Province
	 * Param: province name
	 */
	public function search(){
		$str = $_POST['txt-search'];
		if($str == "") $this->redirect('');
		$this->SetLayout('default');
		$obj = $this -> LoadModel('mapManagement');
		$records = $obj -> getRecords(0,$str);	
		$this -> SetVar('records',$records);
		//$this->set('page_title','List Users');
		$this->SetVar('str',$str);
		$this -> SetVar('page_title', "List Provinces");
		$this -> SetVar('menu_focus', 'list-provinces');
		$this->view('index', 'left-menu');
	}
	/*
	 * Add new province info
	 */
	public function add_province(){
		if(empty($_POST)){
			$this -> SetLayout('default');
			$this -> SetVar('page_title', "Add New Province");
			$this -> SetVar('menu_focus', 'list-provinces');
			$this -> view('add-province', 'left-menu');
		}else{
			$_POST['data']['province']['created_by'] = $_SESSION['user']['id'];
			$_POST['data']['province']['modified_by'] = $_SESSION['user']['id'];
			$this -> Save(($this -> data()));
			$this -> Redirect('');
		}
	}
	/*
	 * Edit Province info
	 * Param: province id
	 */
	public function edit_province($pro_id = 0){		
		if(empty($_POST)){
			if($pro_id != 0){
				$this -> SetLayout('default');
				$this -> SetVar('page_title', "Update Province");
				$obj = $this -> LoadModel('mapManagement');
				$records = $obj -> getRecords($pro_id);
				$this -> SetVar('record', $records[0]);
				$this -> SetVar('menu_focus', 'list-provinces');
				$this -> view('edit-province', 'left-menu');
			}else{
				$this -> Redirect('');
			}
		}else{			
			$_POST['data']['province']['modified_by'] = $_SESSION['user']['id'];
			$this -> Update(($this -> data()),false);
			$this -> Redirect('');
		}	
	}
	/*
	 * Delete province
	 * Param: province id
	 */
	public function delete_province($pro_id = 0){
		$obj = $this -> LoadModel('mapManagement');
		$user = $obj->deleteProvince($pro_id);	
		$this->redirect('');
	}
	/* District */
	public function districts($page_number = ""){
		if(!isset($_SESSION['user'])) $this->redirect('login');
		$this->SetLayout('default');
		
		$province_id = isset($_POST['cbo-province'])? getURL('cbo-province'): @$_SESSION['criteria']['province_id'];
		$province_name = isset($_POST['cbo-province-name'])? getURL('cbo-province-name'): @$_SESSION['criteria']['province_name'];
		$str = isset($_POST['txt-search'])? getURL('txt-search'): @$_SESSION['criteria']['str'];
		if(isset($_SESSION['criteria'])){unset($_SESSION['criteria']);}		
		if($str == "" && $province_id == "" && $province_name == ""){unset($_SESSION['criteria']);}
		
		$obj = $this -> LoadModel('mapManagement');
		
		$limit_start = 0;// for pagination
		$limit_end = 15;// for pagination
		$page_number = $page_number != ""?$page_number:1;// for pagination
		if($page_number != 1)// for pagination
			$limit_start = $limit_end * $page_number - $limit_end;	// for pagination
		
		$results = $obj->getDistrictRecords($province_id, 0, $str, $limit_start, $limit_end);
		$records = $results[0];
		$number_of_rows = $results[1];// for pagination
			
		$this->SetVar('page_number',$page_number);// for pagination
		$this->SetVar('limit_start',$limit_start);// for pagination
		$this->SetVar('limit_end',$limit_end);// for pagination
		$this->SetVar('number_of_rows',$number_of_rows);// for pagination
		
		$provinces = $this -> Query("SELECT `id`, `province_name` FROM `tbl_province` ORDER BY  `province_name` ASC");
		//$records = $obj -> getDistrictRecords();
		$this -> SetVar('provinces', $provinces);
		$this -> SetVar('records',$records);
		$this -> SetVar('str',$str);
		$this -> SetVar('province_id', $province_id);
		$this -> SetVar('province_name', $province_name);
		$this -> SetVar('page_title', "Districts Information");
		$this -> SetVar('menu_focus', 'list-districts');
		
		$_SESSION['criteria']['str'] = $str;
		$_SESSION['criteria']['province_id'] = $province_id;
		$_SESSION['criteria']['province_name'] = $province_name;
		$_SESSION['criteria']['page_number'] = $page_number;
		
		$this -> View('district', 'left-menu');
	}
	/*
	 * Add new district info
	*/
	public function add_district(){
		if(empty($_POST)){
			$this -> SetLayout('default');
			$this -> SetVar('page_title', "Add New District");
			$obj = $this -> LoadModel('mapManagement');
			$provinces = $this -> Query("SELECT `id`, `province_name` FROM `tbl_province` ORDER BY  `province_name` ASC");
			$this -> SetVar('provinces', $provinces);
			$this -> SetVar('menu_focus', 'list-districts');
			$this -> view('add-district', 'left-menu');
		}else{
			$_POST['data']['district']['created_by'] = $_SESSION['user']['id'];
			$_POST['data']['district']['modified_by'] = $_SESSION['user']['id'];
			$this -> Save(($this -> data()));
			$this -> Redirect('districts');
		}
	}
	/*
	 * Edit District info
	* Param: district id
	*/
	public function edit_district($dis_id = 0){
		if(empty($_POST)){
			if($dis_id != 0){
				$this -> SetLayout('default');
				$this -> SetVar('page_title', "Update District");
				$obj = $this -> LoadModel('mapManagement');
				$provinces = $this -> Query("SELECT `id`, `province_name` FROM `tbl_province` ORDER BY  `province_name` ASC");				
				$records = $obj -> getDistrictRecords(0,$dis_id,"",0,0);
				$this -> SetVar('record', $records[0][0]);
				$this -> SetVar('provinces', $provinces);
				$this -> SetVar('menu_focus', 'list-districts');
				$this -> view('edit-district', 'left-menu');
			}else{
				$this -> Redirect('districts');
			}
		}else{
			$_POST['data']['district']['modified_by'] = $_SESSION['user']['id'];
			$this -> Update(($this -> data()),false);
			$this -> Redirect('districts');
		}
	}
	/*
	 * Delete province
	* Param: province id
	*/
	public function delete_district($pro_id = 0){
		$obj = $this -> LoadModel('mapManagement');
		$obj->deleteDistrict($pro_id);
		$this->redirect('districts');
	}	
}
?>